package com.lunwen.view;

import com.lunwen.dao.UserDao;
import com.lunwen.domain.User;
import com.lunwen.utils.StringUtils;
import com.lunwen.view.component.MassageTip;
import com.lunwen.view.component.MeFrame;
import com.lunwen.view.value.ColorValue;
import com.lunwen.view.value.LoginValue;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginFrame extends MeFrame implements MouseListener, DocumentListener {

    private JLabel title = new JLabel("论文管理系统");

    private JTextField acc = new JTextField(10);
    private JPasswordField pass = new JPasswordField(10);
    private JButton login = new JButton("登录");

    private UserDao dao = new UserDao();


    private MeFrame operation = null;

    public LoginFrame() {
        super("管理员登录",600, 450);

        title.setBounds(210,60,350,50);
        context.add(title,28);
        title.setForeground(Color.RED);

        acc.setBounds(120,130,350,48);
        context.add(acc," 用户名",100);
        acc.getDocument().addDocumentListener(this);

        pass.setEchoChar('*');
        pass.setBounds(120,210,350,48);
        context.add(pass," 密码",100);

        login.setBounds(120,300,350,42);
        context.add(login,20);

        login.addActionListener(this);
    }

    @Override
    public void doActionPerformed(ActionEvent e) {
        if (e.getSource() == login){
//            User user1 = new User();
//            user1.setName("admin");
//            operation = new RootFrame(this,user1);
//            this.setVisible(false);
//            operation.setVisible(true);

            String name = acc.getText();
            String password = new String(pass.getPassword());
            if (!StringUtils.isEmpty(name)){
                if (!StringUtils.isEmpty(password)){
                    List<User> query = dao.findByName(name);
                    User user = query.get(0);
                    System.out.println(query);
                    if (query.size() > 0 && password.equals(user.getPassword())){
                        operation = new RootFrame(this,user);
                        this.setVisible(false);
                        operation.setVisible(true);
                    }
                        LoginValue.name = query.get(0).getName();
                        LoginValue.id = query.get(0).getUid();
                    }else {
                        MassageTip.showMassage(this,"账户或密码错误");
                    }
                }else{
                    MassageTip.showMassage(this,"密码不能为空");
                }
            }else {
                MassageTip.showMassage(this,"账户不能为空");
            }
        }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    public void check(DocumentEvent e){
        if (e.getDocument() == acc.getDocument()) {
            if (acc.getText().matches("[0-9]+")) {
                if (acc.getText().length() != 9) {
                    acc.setForeground(Color.RED);
                }else {
                    acc.setForeground(ColorValue.ContextForeground);
                }
            }else {
                if (acc.getText().length() >= 2 && acc.getText().length() < 10) {
                    acc.setForeground(ColorValue.ContextForeground);
                }else {
                    acc.setForeground(Color.RED);
                }
            }
        }
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        check(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        check(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {

    }
}
